package net.wwang.open.controller;

import net.wwang.open.global.ResponseResult;
import net.wwang.open.pojo.Menu;
import net.wwang.open.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author WangWei
 * @date 2020/8/11
 */
@Api(tags = {"目录接口"})
@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuPortalApi {
	@Autowired
	private MenuService menuService;

	/**
	 * 增加目录节点
	 *
	 * @param menu
	 * @return
	 */
	@ApiOperation(value = "增加目录")
	@PostMapping
	public ResponseResult addMenu(@ApiParam(value = "所需参数（type:0表示目录 1表示文档）：{\"name\":\" \",\"space\":\" \",\"type\":\" \",\"parentId\":\" \"}", required = true)@Valid @RequestBody Menu menu) {
		return menuService.addMenu(menu);
	}

	/**
	 * 获取所有目录信息
	 *
	 * @param space
	 * @return
	 */
	@ApiOperation(value = "获取所有目录信息")
	@GetMapping("/index/{space}")
	public ResponseResult getMenus(@ApiParam(value = "文档空间", required = true) @PathVariable("space") String space,
	                               @ApiParam(value = "参数值：name ,create_time,update_time", required = true) @RequestParam(name = "order", required = false) String order,
	                               @ApiParam(value = "参数值：asc ,desc", required = true) @RequestParam(name = "sort", required = false) String sort) {
		return menuService.getAllMenus(space, order, sort);
	}

	/**
	 * 删除目录
	 *
	 * @param menuId
	 * @return
	 */
	@ApiOperation(value = "删除目录")
	@DeleteMapping("/{menuId}")
	public ResponseResult deleteMenu(@PathVariable("menuId") String menuId) {
		return menuService.deleteMenu(menuId);
	}

	/**
	 * 修改目录
	 *
	 * @param menuId
	 * @param menu
	 * @return
	 */
	@ApiOperation(value = "修改目录")
	@PutMapping("/{menuId}")
	public ResponseResult updateMenu(@PathVariable("menuId") String menuId, @Valid@RequestBody Menu menu) {
		return menuService.updateMenu(menuId, menu);
	}
}
